package com.sinoglobal.ningxia.upload;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.amusement.BusinessDetailActivity;
import com.sinoglobal.ningxia.frame.FlyApplication;


/**
 * 
 * @author wxy
 * @createdate 2014-5-24 上午11:42:03
 * @Description: TODO(选择对文件的操作：设置为默认、隐藏、删除、取消等)
 */
public class SelectPicActivity extends Activity implements OnClickListener {

	// 使用照相机拍照获取图片
	public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
	// 使用相册中的图片
	public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
	// 从Intent获取图片路径的KEY
	public static final String KEY_PHOTO_PATH = "photo_path";
	private static final String TAG = "SelectPicActivity";
	private LinearLayout dialogLayout;
	private Button takePhotoBtn, pickPhotoBtn, cancelBtn, delBtn;

	//获取到的图片路径 
	private String picPath;
	private Intent lastIntent;
	private Uri photoUri;

	private int addFlag = 1;
	String type;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_pic_layout); 

//		dialogLayout = (LinearLayout) findViewById(R.id.dialog_layout);
//		dialogLayout.setOnClickListener(this);
		delBtn = (Button) findViewById(R.id.tv10);
		delBtn.setOnClickListener(this);
		takePhotoBtn = (Button) findViewById(R.id.tv5);
		takePhotoBtn.setOnClickListener(this);
		pickPhotoBtn = (Button) findViewById(R.id.tv6);
		pickPhotoBtn.setOnClickListener(this);
		cancelBtn = (Button) findViewById(R.id.tv4);
		cancelBtn.setOnClickListener(this);
//        btnSetDefault=(Button)findViewById(R.id.btn_1);
//        btnSetDefault.setOnClickListener(this);
//        btnSetHide=(Button)findViewById(R.id.btn_2);
//        btnSetHide.setOnClickListener(this);
        
		lastIntent = getIntent();
		addFlag = lastIntent.getExtras().getInt("addFlag");
		type=lastIntent.getStringExtra("type");
//		System.out.println("addFlag="+addFlag);
		if (addFlag == 1) { // 替换，，须显示删除按钮
			delBtn.setVisibility(View.VISIBLE);
			pickPhotoBtn.setVisibility(View.GONE);
			takePhotoBtn.setVisibility(View.GONE);
//			if("1".equals(type)){
//				btnSetDefault.setText("取消默认");
//			}else if("2".equals(type)){//如果是隐藏就显示变暗背景 并且设置为 "隐藏" 
//				btnSetHide.setText("取消隐藏");
//			}
		} else {
			delBtn.setVisibility(View.GONE);
//			btnSetDefault.setVisibility(View.GONE);
//			btnSetHide.setVisibility(View.GONE);
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv10:
			setResult(Activity.RESULT_FIRST_USER);
			finish();
		case R.id.tv4:
			finish();
			break;
		case R.id.tv5:
			takePhoto();
			break;
		case R.id.tv6:
//			pickPhoto();
			startActivity(new Intent(this,PicListActivity.class));
			finish();
			break;
		case R.id.btn_1://设为默认
			if("1".equals(type)){
				Toast.makeText(FlyApplication.context, "成功取消默认", 0).show();
				setResult(BusinessDetailActivity.SET_NORMAL_PHOTO);
			}else {
				Toast.makeText(FlyApplication.context, "成功设为默认", 0).show();
				setResult(BusinessDetailActivity.SET_DEFAULT_PHOTO);
			}
			finish();
			break;
		case R.id.btn_2://设为隐藏
			if("2".equals(type)){//如果是隐藏就显示变暗背景 并且设置为 "隐藏" 
				Toast.makeText(FlyApplication.context, "成功取消隐藏", 0).show();
				setResult(BusinessDetailActivity.SET_NORMAL_PHOTO);
			}else{
				Toast.makeText(FlyApplication.context, "成功设为隐藏", 0).show();
				setResult(BusinessDetailActivity.SET_HIDE_PHOTO);
			}
			finish();
			break;
		default:
			finish();
			break;
		}
	}

	/**
	 * 拍照获取图片
	 */
	private void takePhoto() {
		// 执行拍照前，应该先判断SD卡是否存在
		String SDState = Environment.getExternalStorageState();
		if (SDState.equals(Environment.MEDIA_MOUNTED)) {

			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
			/***
			 * 以下操作使用照相机拍照，拍照后的图片会存放在相册中?这里使用的这种方式的好处就是获取的图片是拍照后的原图
			 * 如果不实用ContentValues存放照片路径的话，拍照后获取的图片为缩略图不清晰
			 */
			ContentValues values = new ContentValues();
			photoUri = this.getContentResolver().insert(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
			intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
			/** ----------------- */
			startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
		} else {
			Toast.makeText(FlyApplication.context, "内存卡不存在,无法获得图片！", Toast.LENGTH_LONG).show();
		}
	}

	/***
	 * 从相册中取图片
	 */
	private void pickPhoto() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return super.onTouchEvent(event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			doPhoto(requestCode, data);
		}
		super.onActivityResult(requestCode, resultCode, data);
		
	}

	/**
	 * 选择图片后，获取图片的路片
	 * 
	 * @param requestCode
	 * @param data
	 */
	private void doPhoto(int requestCode, Intent data) {
		if (requestCode == SELECT_PIC_BY_PICK_PHOTO) // 从相册取图片，有些手机有异常情况，请注意
		{
			if (data == null) {
				Toast.makeText(FlyApplication.context, "选择图片文件出错，请重试！", Toast.LENGTH_LONG).show();
				return;
			}
			photoUri = data.getData();
			if (photoUri == null) {
				Toast.makeText(FlyApplication.context, "选择图片文件出错，请重试！", Toast.LENGTH_LONG).show();
				return;
			}
		}
		String[] pojo = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
		if (cursor != null) {
			int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
			cursor.moveToFirst();
			picPath = cursor.getString(columnIndex);
			cursor.close();
		}
		Log.i(TAG, "imagePath = " + picPath);
		/*
		 * if(picPath != null && ( picPath.endsWith(".png") ||
		 * picPath.endsWith(".PNG") ||picPath.endsWith(".jpg")
		 * ||picPath.endsWith(".JPG") ))
		 */
		if (picPath != null) {
			lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
			setResult(Activity.RESULT_OK, lastIntent);
			finish();
		} else {
			Toast.makeText(FlyApplication.context, "选择文件不正确，请重试！", Toast.LENGTH_LONG).show();

		}
	}
}
